package com.zlp.mall.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zlp.mall.search.domain.EsSpu;
import com.zlp.mall.search.domain.Spu;
import com.zlp.mall.search.repository.EsSpuRepository;
import com.zlp.mall.search.service.EsSpuService;
import com.zlp.mall.search.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;


/**
 * 搜索商品管理Service实现类
 * @since 2021-11-3 10:46:46
 */
@Service
@Slf4j(topic = "EsSpuServiceImpl")
public class EsSpuServiceImpl implements EsSpuService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EsSpuServiceImpl.class);

    @Resource
    private SpuService spuService;

    @Autowired
    private EsSpuRepository spuRepository;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    private static final String PRE_TAG = "<font color='red'>";//google的色值
    private static final String POST_TAG = "</font>";

    @Override
    public int importAll(Integer id) {

        log.info("importAll");
        LambdaQueryWrapper<Spu> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(id)) {
            queryWrapper.eq(Spu::getId,1L);
        }
        List<Spu> list = spuService.list(queryWrapper);
        List<EsSpu> esSpuList = new ArrayList<>();
        for (Spu spu : list) {
            EsSpu esSpu = new EsSpu();
            BeanUtils.copyProperties(spu,esSpu);
            esSpu.setUpTime(spu.getUpTime().getTime()   );
            esSpuList.add(esSpu);
        }
        Iterable<EsSpu> esProductIterable = spuRepository.saveAll(esSpuList);

        Iterator<EsSpu> iterator = esProductIterable.iterator();
        int result = 0;
        while (iterator.hasNext()) {
            result++;
            iterator.next();
        }
        log.info("importAll.resp result={}", result);
        return result;
    }

    @Override
    public void delete(Long id) {

        log.info("delete.req id={}", id);
        spuRepository.deleteById(id);
    }

    @Override
    public EsSpu create(Long id) {

        EsSpu result = null;
        Spu spu = spuService.getById(id);
        if (Objects.nonNull(spu)) {
            BeanUtils.copyProperties(spu,result);
            result = spuRepository.save(result);
        }
        return result;
    }

    @Override
    public void delete(List<Long> ids) {

    }

    @Override
    public Page<EsSpu> search(String keyword, Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public Page<EsSpu> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize, Integer sort) {
        return null;
    }




}
